package org.jeecg.modules.zcgl.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.modules.system.mapper.SysDepartMapper;
import org.jeecg.modules.zcgl.entity.CustomerBalance;
import org.jeecg.modules.zcgl.entity.CustomerBalanceChangesDetail;
import org.jeecg.modules.zcgl.entity.CustomerInfo;
import org.jeecg.modules.zcgl.entity.LeaseContractInfo;
import org.jeecg.modules.zcgl.enums.ChangesTypeEnum;
import org.jeecg.modules.zcgl.mapper.CustomerBalanceChangesDetailMapper;
import org.jeecg.modules.zcgl.mapper.CustomerBalanceMapper;
import org.jeecg.modules.zcgl.mapper.CustomerInfoMapper;
import org.jeecg.modules.zcgl.mapper.LeaseContractInfoMapper;
import org.jeecg.modules.zcgl.service.ICustomerBalanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.List;

/**
 * @Description: 客户余额信息
 * @Author: jeecg-boot
 * @Date: 2020-12-11
 * @Version: V1.0
 */
@Transactional
@Service
public class CustomerBalanceServiceImpl extends ServiceImpl<CustomerBalanceMapper, CustomerBalance> implements ICustomerBalanceService {

    @Resource
    private CustomerBalanceChangesDetailMapper customerBalanceChangesDetailMapper;
    @Resource
    private SysDepartMapper sysDepartMapper;
    @Resource
    private CustomerInfoMapper customerInfoMapper;
    @Resource
    private LeaseContractInfoMapper leaseContractInfoMapper;

    @Override
    @Transactional
    public void saveMain(CustomerBalance customerBalance, List<CustomerBalanceChangesDetail> customerBalanceChangesDetailList) {
        baseMapper.insert(customerBalance);
        if (customerBalanceChangesDetailList != null && customerBalanceChangesDetailList.size() > 0) {
            for (CustomerBalanceChangesDetail entity : customerBalanceChangesDetailList) {
                //外键设置
                entity.setCustomerBalanceId(customerBalance.getId());
                customerBalanceChangesDetailMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional
    public void updateMain(CustomerBalance customerBalance, List<CustomerBalanceChangesDetail> customerBalanceChangesDetailList) {
        baseMapper.updateById(customerBalance);

        //1.先删除子表数据
        customerBalanceChangesDetailMapper.deleteByMainId(customerBalance.getId());

        //2.子表数据重新插入
        if (customerBalanceChangesDetailList != null && customerBalanceChangesDetailList.size() > 0) {
            for (CustomerBalanceChangesDetail entity : customerBalanceChangesDetailList) {
                //外键设置
                entity.setCustomerBalanceId(customerBalance.getId());
                customerBalanceChangesDetailMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional
    public void delMain(String id) {
        customerBalanceChangesDetailMapper.deleteByMainId(id);
        baseMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for (Serializable id : idList) {
            customerBalanceChangesDetailMapper.deleteByMainId(id.toString());
            baseMapper.deleteById(id);
        }
    }

    @Override
    public CustomerBalance selectCustomerIdAndCompanyId(String companyId, String customerId) {
        return baseMapper.selectCustomerIdAndCompanyId(companyId, customerId);
    }

    @Override
    public CustomerBalance selectCustomerIdAndLeaseContractId(String leaseContractId, String customerId) {
        return baseMapper.selectCustomerIdAndLeaseContractId(leaseContractId,customerId);
    }

    @Override
    public BigDecimal selectBalanceCustomerIdAndCompanyId(String companyId, String customerId) {
        return baseMapper.selectBalanceCustomerIdAndCompanyId(companyId, customerId);
    }

    @Override
    @Transactional
    public boolean updateBalanceByCustomerIdAndCompanyId(String companyId, String customerId, BigDecimal amountMoney, boolean type, String handleId, Integer handleType, String description,String receivablesId) {

        CustomerInfo customerInfo = customerInfoMapper.selectById(customerId);

        if (customerInfo == null) {
            throw new JeecgBootException("客户信息错误");
        }
        if (customerInfo.getTotalBalance() == null) {
            customerInfo.setTotalBalance(BigDecimal.ZERO);
        }

        CustomerBalance customerBalance = baseMapper.selectCustomerIdAndCompanyId(companyId, customerId);
        //修改用户余额的信息
        if (customerBalance == null) {
            customerBalance = new CustomerBalance();
            customerBalance.setCustomerId(customerId);
            customerBalance.setCompanyId(companyId);
            customerBalance.setBalance(BigDecimal.ZERO);
            customerBalance.setSysOrgCode(sysDepartMapper.selectOrgCodeById(companyId));
        }

        CustomerBalanceChangesDetail detail = new CustomerBalanceChangesDetail();
        detail.setOldBalance(customerBalance.getBalance());

        //根据增加减少设置余额
        if (type) {
            customerInfo.setTotalBalance(customerInfo.getTotalBalance().add(amountMoney).setScale(2, BigDecimal.ROUND_HALF_UP));
            customerBalance.setBalance(customerBalance.getBalance().add(amountMoney).setScale(2, BigDecimal.ROUND_HALF_UP));
            detail.setChangesType(ChangesTypeEnum.ADD.getStatus());
        } else {
            customerBalance.setBalance(customerBalance.getBalance().subtract(amountMoney).setScale(2, BigDecimal.ROUND_HALF_UP));
            customerInfo.setTotalBalance(customerInfo.getTotalBalance().subtract(amountMoney).setScale(2, BigDecimal.ROUND_HALF_UP));
            detail.setChangesType(ChangesTypeEnum.LESSEN.getStatus());
        }
        boolean bool = super.saveOrUpdate(customerBalance);

        if (!bool) {
            return false;
        }
        detail.setChangesAmount(amountMoney);
        detail.setDescription(description);
        detail.setReceivablesId(receivablesId);
        detail.setHandleType(handleType);
        detail.setHandleId(handleId);
        detail.setCustomerBalanceId(customerBalance.getId());
        detail.setCustomerId(customerBalance.getCustomerId());
        detail.setCompanyId(customerBalance.getCompanyId());
        detail.setSysOrgCode(customerBalance.getSysOrgCode());
        detail.setNewBalance(customerBalance.getBalance());
        customerBalanceChangesDetailMapper.insert(detail);

        return super.retBool(customerInfoMapper.updateById(customerInfo));
    }

    @Override
    public boolean updateBalanceByCustomerIdAndLeaseContractId(String companyId, String leaseContractId, String customerId, BigDecimal amountMoney, boolean type, String handleId, Integer handleType, String description, String receivablesId) {

        LeaseContractInfo leaseContractInfo = leaseContractInfoMapper.selectById(leaseContractId);

        if (leaseContractInfo == null) {
            throw new JeecgBootException("查询合同信息错误");
        }
        if (leaseContractInfo.getTotalBalance() == null) {
            leaseContractInfo.setTotalBalance(BigDecimal.ZERO);
        }

        CustomerBalance customerBalance = baseMapper.selectCustomerIdAndLeaseContractId(leaseContractId, customerId);
        //修改用户余额的信息
        if (customerBalance == null) {
            customerBalance = new CustomerBalance();
            customerBalance.setCustomerId(customerId);
            customerBalance.setCompanyId(companyId);
            customerBalance.setLeaseContractId(leaseContractId);
            customerBalance.setBalance(BigDecimal.ZERO);
            customerBalance.setSysOrgCode(sysDepartMapper.selectOrgCodeById(companyId));
        }

        CustomerBalanceChangesDetail detail = new CustomerBalanceChangesDetail();
        detail.setOldBalance(customerBalance.getBalance());

        //根据增加减少设置余额
        if (type) {
            customerBalance.setBalance(customerBalance.getBalance().add(amountMoney).setScale(2, BigDecimal.ROUND_HALF_UP));
            detail.setChangesType(ChangesTypeEnum.ADD.getStatus());
        } else {
            customerBalance.setBalance(customerBalance.getBalance().subtract(amountMoney).setScale(2, BigDecimal.ROUND_HALF_UP));
            detail.setChangesType(ChangesTypeEnum.LESSEN.getStatus());
        }
        leaseContractInfo.setTotalBalance(customerBalance.getBalance());
        boolean bool = super.saveOrUpdate(customerBalance);

        if (!bool) {
            return false;
        }
        detail.setChangesAmount(amountMoney);
        detail.setDescription(description);
        detail.setReceivablesId(receivablesId);
        detail.setHandleType(handleType);
        detail.setHandleId(handleId);
        detail.setCustomerBalanceId(customerBalance.getId());
        detail.setCustomerId(customerBalance.getCustomerId());
        detail.setCompanyId(customerBalance.getCompanyId());
        detail.setSysOrgCode(customerBalance.getSysOrgCode());
        detail.setNewBalance(customerBalance.getBalance());
        customerBalanceChangesDetailMapper.insert(detail);

        return super.retBool(leaseContractInfoMapper.updateById(leaseContractInfo));
    }

    @Override
    public List<CustomerBalance> getByCustomerId(String customerId) {
        return baseMapper.getByCustomerId(customerId);
    }


}
